package OSMS.action.goodsfunc;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Car;
import OSMS.entity.Customers;
import OSMS.entity.Goods;
public class updateGoodsCarServlet extends HttpServlet {
	
	GoodsDao gdao = new GoodsDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
		
		System.out.println("走到这了");
		HttpSession session = request.getSession();
		Customers cus = (Customers)session.getAttribute("cus");
		int cid = cus.getCid();
		int gid;
		
		ArrayList<Car>  carList= new ArrayList<Car>();
		carList = (ArrayList<Car>) session.getAttribute("carList");
		int carNum = carList.size();
		boolean flag_update;
		
		Goods tmp_good = new Goods();
		for(int i = 0;i < carNum;i++)
		{
			gid = carList.get(i).getGid();					//获取gid
			String para = "number_"+Integer.toString(i);	//拼接前台数量框的框名
			String tmp =request.getParameter(para);			//获取字符型商品数量
			int gnum=Integer.parseInt(tmp);					//商品数量转为int型
			try {
				tmp_good = gdao.getGoodsById(gid);			//获取该商品的对象
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int gstore = tmp_good.getGstore();				//获取该商品的库存
			if(gstore<gnum)
			{
				 request.getRequestDispatcher("../goods/fail_to_update_goods_num.jsp").forward(request, response);
				return;
			}
			
		}
		for(int i = 0;i < carNum;i++)
		{
			gid = carList.get(i).getGid();
			String para = "number_"+Integer.toString(i);
			String tmp =request.getParameter(para);
			int gnum=Integer.parseInt(tmp);
			try {
				flag_update = gdao.updateCarGoods(gnum, cid, gid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/user/showcar");
		
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
